package com.xiaobao.coalproperty.module.user.view;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.module.user.model.entity.BankTypeEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.util.UserHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

//提现资料
public class WithdrawDataActivity extends BaseActivity {

    @BindView(R.id.withdraw_data_name)
    EditText edtName;
    @BindView(R.id.withdraw_data_card_num)
    EditText edtCardNum;
    @BindView(R.id.withdraw_data_bank)
    TextView tvBank;

    private List<String> bankList;
    private OptionsPickerView mOptionsPickerView;

    private String bankName;

    @Override
    protected int getContentView() {
        return R.layout.activity_withdraw_data;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("提现资料设置");
        bankType();

        if (UserHelperUtils.getInstance().getCenter() != null) {
            if (!UserHelperUtils.getInstance().getCenter().getData().getAccount_person().equals("")) {
                edtCardNum.setText(UserHelperUtils.getInstance().getCenter().getData().getBank_account());
                edtName.setText(UserHelperUtils.getInstance().getCenter().getData().getAccount_person());
                tvBank.setText(UserHelperUtils.getInstance().getCenter().getData().getBank_name());
            }
        }
        mOptionsPickerView = new OptionsPickerView.Builder(this, mOnOptionsSelectListener).build();
    }

    public void onWithDrawClick(View view) {
        OkHttpUtils.post().url(UrlConstant.URL_SET_BANK_INFO)
                .params(HttpHelperUtils.getInstance().setBankInfoParams(edtName.getText().toString(), tvBank.getText().toString(), edtCardNum.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    public void onWithDrawBankClick(View view) {
        if (bankList != null) {
            mOptionsPickerView.setPicker(bankList);
            mOptionsPickerView.show();
        }
    }

    private OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            bankName = bankList.get(options1);
            tvBank.setText(bankName);
        }
    };

    private void bankType() {
        OkHttpUtils.post().url(UrlConstant.URL_BANKTYPE)
                .params(HttpHelperUtils.getInstance().indexParams())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BankTypeEntity entity = new Gson().fromJson(response, BankTypeEntity.class);
                    if (entity.getCode() == 0) {
                        bankList = new ArrayList<>();
                        for (BankTypeEntity.DataBean bean : entity.getData()) {
                            bankList.add(bean.getName());
                        }
                    }
                } catch (Exception e) {
                }
            }
        });
    }
}
